package com.example.kotlin.activity

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import com.example.kotlin.R
import com.example.kotlin.service.MyService
import kotlinx.android.synthetic.main.activity_service.bindServiceBtn
import kotlinx.android.synthetic.main.activity_service.startServiceBtn
import kotlinx.android.synthetic.main.activity_service.stopServiceBtn
import kotlinx.android.synthetic.main.activity_service.unbindServiceBtn

class ServiceActivity : BaseActivity() {

    lateinit var downloadBinder:MyService.DownloadBinder

    private val connection=object:ServiceConnection{
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            downloadBinder=service as MyService.DownloadBinder
            downloadBinder.startDownload()
            downloadBinder.getProgess()
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            TODO("Not yet implemented")
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_service)

        startServiceBtn.setOnClickListener {
            val intent=Intent(this,MyService::class.java)
            startService(intent)
        }

        stopServiceBtn.setOnClickListener {
            val intent=Intent(this,MyService::class.java)
            stopService(intent)
        }

        bindServiceBtn.setOnClickListener {
            val intent=Intent(this,MyService::class.java)
            bindService(intent,connection,Context.BIND_AUTO_CREATE)
        }

        unbindServiceBtn.setOnClickListener {
            unbindService(connection)
        }

    }
}